package com.jhhc.StormSurgeForecast.StormSurge.Uitl;

import com.jhhc.RHDB.Data.Pojo.Data;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.RealTimeHydrologicData;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.PointResult;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ArithmeticUtil {


    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Calendar calendar = new GregorianCalendar();

    //twc
    public  List<Data>  genInterpolation(String startTime,String endTime,Double a,Double c,Integer stepLength,String type  ){
        List<Data> dataList = new ArrayList<>();
        Date date1 = null;
        long diff = 0;
        try {
            date1 =  format.parse(startTime);
            Date date2 =  format.parse(endTime);
            //从对象中拿到时间
            long d1 =date1.getTime();
            long d2 = date2.getTime();
            diff=(d2-d1)/1000/60;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //System.out.println("开始时间："+startTime +"结束时间"+endTime +"a:" +a +"c:"+c);
        Double k;
        Double b;
        Double y ;
        b=a;
        for (int i=1;i<diff/stepLength;i++){
            Data data = new Data();
            calendar.setTime(date1);
            calendar.add(Calendar.MINUTE,stepLength*i);
            String time  = format.format(calendar.getTime());
            //斜率
            k=(c-a)/(diff/stepLength);
            //获取值
            y=(i*k)+b;
            data.setTM(time);
            switch (type){

                case "twc":
                    data.setValue(Double.valueOf(String.format("%.2f",y)));
                    break;
                case "zs":
                    data.setValue(Double.valueOf(String.format("%.2f",y)));
                    break;
                case "ftdz":
                    data.setValue(Double.valueOf(String.format("%.2f",y)));
                    break;

                default:
            }
            dataList.add(data);
            dataList.sort(Comparator.comparing(Data::getTM));
        }
        return dataList;
    }

    //线性方程
    public  List<PointResult>  getInterpolation(String startTime,String endTime,Double a,Double c,Integer stepLength,String type  ){
        List<PointResult> pointResults = new ArrayList<>();
        Date date1 = null;
        long diff = 0;
        try {
            date1 =  format.parse(startTime);
            Date date2 =  format.parse(endTime);
            //从对象中拿到时间
            long d1 =date1.getTime();
            long d2 = date2.getTime();
            diff=(d2-d1)/1000/60;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //System.out.println("开始时间："+startTime +"结束时间"+endTime +"a:" +a +"c:"+c);
        Double k;
        Double b;
        Double y ;
        b=a;
        for (int i=1;i<diff/stepLength;i++){
            PointResult pointResult = new PointResult();
            calendar.setTime(date1);
            calendar.add(Calendar.MINUTE,stepLength*i);
            String time  = format.format(calendar.getTime());
            //斜率
            k=(c-a)/(diff/stepLength);
            //获取值
            y=(i*k)+b;
            pointResult.setTm(time);
            switch (type){

                case "twc":
                    pointResult.setTwc(Double.valueOf(String.format("%.2f",y)));
                    break;
                case "zs":
                    pointResult.setZs(Double.valueOf(String.format("%.2f",y)));
                    break;
                case "ftdz":
                    pointResult.setFtdz(Double.valueOf(String.format("%.2f",y)));
                    break;

                default:
            }
            pointResults.add(pointResult);
            pointResults.sort(Comparator.comparing(PointResult::getTm));
        }
        return pointResults;
    }

    //实测 线性方程
    public  List<RealTimeHydrologicData>  getInterpolationTDZ(String startTime, String endTime, Double a, Double c, Integer stepLength){
        List<RealTimeHydrologicData> realTimeHydrologicDataList = new ArrayList<>();
        Date date1 = null;
        long diff = 0;
        try {
            date1 =  format.parse(startTime);
            Date date2 =  format.parse(endTime);
            //从对象中拿到时间
            long d1 =date1.getTime();
            long d2 = date2.getTime();
            diff=(d2-d1)/1000/60;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //System.out.println("开始时间："+startTime +"结束时间"+endTime +"a:" +a +"c:"+c);
        Double k;
        Double b;
        Double y ;
        b=a;
        for (int i=1;i<diff/stepLength;i++){
            RealTimeHydrologicData realTimeHydrologicData = new RealTimeHydrologicData();
            calendar.setTime(date1);
            calendar.add(Calendar.MINUTE,stepLength*i);
            String time  = format.format(calendar.getTime());
            //斜率
            k=(c-a)/(diff/stepLength);
            //获取值
            y=(i*k)+b;
            realTimeHydrologicData.setTime(time);
            realTimeHydrologicData.setTdz(Double.valueOf(String.format("%.2f",y)));
            realTimeHydrologicDataList.add(realTimeHydrologicData);
            realTimeHydrologicDataList.sort(Comparator.comparing(RealTimeHydrologicData::getTime));
        }
        return realTimeHydrologicDataList;
    }

    public  List<Data>  getTideTdz(String startTime, String endTime, Double a, Double c, Integer stepLength){
        List<Data> realTimeHydrologicDataList = new ArrayList<>();
        Date date1 = null;
        long diff = 0;
        try {
            date1 =  format.parse(startTime);
            Date date2 =  format.parse(endTime);
            //从对象中拿到时间
            long d1 =date1.getTime();
            long d2 = date2.getTime();
            diff=(d2-d1)/1000/60;
        } catch (Exception e) {
            e.printStackTrace();
        }
        //System.out.println("开始时间："+startTime +"结束时间"+endTime +"a:" +a +"c:"+c);
        Double k;
        Double b;
        Double y ;
        b=a;
        for (int i=1;i<diff/stepLength;i++){
            Data realTimeHydrologicData = new Data();
            calendar.setTime(date1);
            calendar.add(Calendar.MINUTE,stepLength*i);
            String time  = format.format(calendar.getTime());
            //斜率
            k=(c-a)/(diff/stepLength);
            //获取值
            y=(i*k)+b;
            realTimeHydrologicData.setTM(time);
            realTimeHydrologicData.setValue(Double.valueOf(String.format("%.2f",y)));
            realTimeHydrologicDataList.add(realTimeHydrologicData);
            realTimeHydrologicDataList.sort(Comparator.comparing(Data::getTM));
        }
        return realTimeHydrologicDataList;
    }

    public static void main(String[] args) {
        ArithmeticUtil arithmeticUtil =new ArithmeticUtil();

        arithmeticUtil.getInterpolation("2022-12-05 15:00:00","2022-12-05 16:00:00",1.09,2.11,5,"ftdz");
    }



}
